package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system._enum.KfnWorkOrderMaRichemontUpdateMethodEnum;
import com.ruoyi.system.domain.KfnWorkOrder;
import com.ruoyi.system.domain.KfnWorkOrderMaRichemont;
import com.ruoyi.system.domain.KfnWorkOrderMaRichemont;
import com.ruoyi.system.domain.bo.KfnWorkOrderMaRichemontBo;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.KfnProjectMapper;
import com.ruoyi.system.mapper.KfnWorkOrderMaRichemontMapper;
import com.ruoyi.system.mapper.KfnWorkOrderMapper;
import com.ruoyi.system.mapper.KfnWorkOrderStoreRichemontMapper;
import com.ruoyi.system.service.IKfnWorkOrderMaRichemontService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Richemont MA工单Service业务层处理
 *
 * @author Panda
 * @date 2024-01-05
 */
@RequiredArgsConstructor
@Service
public class KfnWorkOrderMaRichemontServiceImpl implements IKfnWorkOrderMaRichemontService {

    private final KfnWorkOrderMaRichemontMapper baseMapper;
    private final KfnWorkOrderStoreRichemontMapper kfnWorkOrderStoreRichemontMapper;
    private final KfnWorkOrderMapper kfnWorkOrderMapper;
    private final KfnProjectMapper kfnProjectMapper;
    /**
     * 查询Richemont MA工单
     */
    @Override
    public KfnWorkOrderMaRichemontVo queryById(String workOrderId){
        return baseMapper.selectVoById(workOrderId);
    }

    /**
     * 查询Richemont MA工单列表
     */
    @Override
    public TableDataInfo<KfnWorkOrderMaRichemontVo> queryPageList(KfnWorkOrderMaRichemontBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnWorkOrderMaRichemont> lqw = buildQueryWrapper(bo);
        Page<KfnWorkOrderMaRichemontVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<KfnWorkOrderMaRichemontVo> kfnWorkOrderMaRichemontVos = result.getRecords();
        //获取所有的工单号
        List<String> workOrderIds = kfnWorkOrderMaRichemontVos.stream().map(KfnWorkOrderMaRichemontVo::getWorkOrderId).collect(Collectors.toList());
        //通过所查询的工单号差工单池内通用信息
        if(CollectionUtil.isEmpty(workOrderIds)){
            return TableDataInfo.build(result);
        }
        Map<String, KfnWorkOrderVo> kfnWorkOrderVoMap = kfnWorkOrderMapper.selectVoBatchIds(workOrderIds).stream().collect(Collectors.toMap(KfnWorkOrderVo::getWorkOrderId, kfnWorkOrderVo -> kfnWorkOrderVo));
        //将通用信息复制到独立品牌的工单上
        kfnWorkOrderMaRichemontVos.forEach(k -> {
            KfnWorkOrderVo kfnWorkOrderVo = kfnWorkOrderVoMap.get(k.getWorkOrderId());
            BeanUtil.copyProperties(kfnWorkOrderVo,k,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        });
        result.setRecords(kfnWorkOrderMaRichemontVos);

        return TableDataInfo.build(result);
    }

    /**
     * 查询Richemont MA工单列表
     */
    @Override
    public List<KfnWorkOrderMaRichemontVo> queryList(KfnWorkOrderMaRichemontBo bo) {
        LambdaQueryWrapper<KfnWorkOrderMaRichemont> lqw = buildQueryWrapper(bo);
        List<KfnWorkOrderMaRichemontVo> kfnWorkOrderMaRichemontVos = baseMapper.selectVoList(lqw);
        //获取所有的工单号
        List<String> workOrderIds = kfnWorkOrderMaRichemontVos.stream().map(KfnWorkOrderMaRichemontVo::getWorkOrderId).collect(Collectors.toList());
        //通过所查询的工单号差工单池内通用信息
        if(CollectionUtil.isEmpty(workOrderIds)){
            return kfnWorkOrderMaRichemontVos;
        }
        Map<String, KfnWorkOrderVo> kfnWorkOrderVoMap = kfnWorkOrderMapper.selectVoBatchIds(workOrderIds).stream().collect(Collectors.toMap(KfnWorkOrderVo::getWorkOrderId, kfnWorkOrderVo -> kfnWorkOrderVo));
        //将通用信息复制到独立品牌的工单上
        kfnWorkOrderMaRichemontVos.forEach(k -> {
            KfnWorkOrderVo kfnWorkOrderVo = kfnWorkOrderVoMap.get(k.getWorkOrderId());
            BeanUtil.copyProperties(kfnWorkOrderVo,k,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        });
        return kfnWorkOrderMaRichemontVos;
    }

    private LambdaQueryWrapper<KfnWorkOrderMaRichemont> buildQueryWrapper(KfnWorkOrderMaRichemontBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnWorkOrderMaRichemont> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getResponseLevel()), KfnWorkOrderMaRichemont::getResponseLevel, bo.getResponseLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairType()), KfnWorkOrderMaRichemont::getRepairType, bo.getRepairType());
        return lqw;
    }

    /**
     * 新增Richemont MA工单
     */
    @Transactional
    @Override
    public Boolean insertByBo(KfnWorkOrderMaRichemontBo bo) {
        KfnWorkOrderMaRichemont add = BeanUtil.toBean(bo, KfnWorkOrderMaRichemont.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWorkOrderId(add.getWorkOrderId());
        }
        //查询店铺信息
        KfnWorkOrderStoreRichemontVo kfnWorkOrderStoreRichemontVo = kfnWorkOrderStoreRichemontMapper.selectVoById(bo.getStoreId());
        BeanUtil.copyProperties(kfnWorkOrderStoreRichemontVo, bo, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        bo.setStoreAddress(kfnWorkOrderStoreRichemontVo.getProvince() + kfnWorkOrderStoreRichemontVo.getCity() + kfnWorkOrderStoreRichemontVo.getDistricts() + kfnWorkOrderStoreRichemontVo.getAddress());
        //查询项目信息
        KfnProjectVo kfnProjectVo = kfnProjectMapper.selectVoById(bo.getProjectId());
        BeanUtil.copyProperties(kfnProjectVo, bo, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        //写入工单池
        KfnWorkOrder kfnWorkOrder = BeanUtil.toBean(bo, KfnWorkOrder.class);
        boolean insert = kfnWorkOrderMapper.insert(kfnWorkOrder) > 0;
        if (insert && flag) {
            return true;
        } else {
            throw new RuntimeException("未知异常，回滚插入");
        }
    }

    /**
     * 修改Richemont MA工单
     */
    @Override
    public Boolean updateByBo(KfnWorkOrderMaRichemontBo bo) {
        KfnWorkOrderMaRichemont update = BeanUtil.toBean(bo, KfnWorkOrderMaRichemont.class);
        validEntityBeforeSave(update);
        if(bo.getMethodName().equals(KfnWorkOrderMaRichemontUpdateMethodEnum.UPDATE.getMethodName())) {
            //修改基础信息
            KfnWorkOrder kfnWorkOrder = BeanUtil.toBean(bo, KfnWorkOrder.class);
            kfnWorkOrderMapper.updateById(kfnWorkOrder);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnWorkOrderMaRichemont entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除Richemont MA工单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        kfnWorkOrderMapper.deleteBatchIds(ids);
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
